package com.mi.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mi.entity.MyPages;
import com.mi.entity.ResponseMsg;
import com.mi.entity.RecruitEntity;
import com.mi.service.RecruitService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mi.entity.ApplicationEntity;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RecruitController {

    @Reference(check = false)
    RecruitService recruitService;

    //招聘列表
    @RequestMapping("recruitList")
    public String recruitList(RecruitEntity recruitEntity, HttpServletRequest request){
        MyPages<RecruitEntity> recruitEntityMyPages = recruitService.queryRecruitss(recruitEntity);
        request.setAttribute("pageInfo",recruitEntityMyPages);
        request.setAttribute("r_position",recruitEntity.getR_position());
        request.setAttribute("r_kind",recruitEntity.getR_kind());
        return "recruitList";
    }

    //招聘详情
    @RequestMapping("applicationDetails")
    public String  applicationDetails(ApplicationEntity applicationEntity,HttpServletRequest request){
        ApplicationEntity applicationEntity1 = recruitService.queryApplication(applicationEntity);
        request.setAttribute("applict",applicationEntity1);
        return "applicationDetails";
    }

    //查询应聘信息
    @RequestMapping("applicationList")
    public String applicationList(ApplicationEntity applicationEntity,HttpServletRequest request){
        MyPages<ApplicationEntity> applicationEntityMyPages = recruitService.queryApplications(applicationEntity);
        request.setAttribute("pageInfo",applicationEntityMyPages);
        request.setAttribute("a_name",applicationEntity.getA_name());
        request.setAttribute("a_sex",applicationEntity.getA_sex());
        request.setAttribute("a_age",applicationEntity.getA_age());
        return "applicationList";
    }

    //删除应聘
    @RequestMapping("delApplication")
    @ResponseBody
    public ResponseMsg delApplication(String ids) {
        ResponseMsg rm = new ResponseMsg();
        String[] r_ids;
        if (ids.contains(",")) {
            r_ids = ids.split(",");
        } else {
            r_ids = new String[]{ids};
        }
        boolean b = recruitService.delApplication(r_ids);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
        }
        return rm;
    }

    //应聘状态更改
    @RequestMapping("updApplicationStatus")
    @ResponseBody
    public ResponseMsg updApplicationStatus(ApplicationEntity applicationEntity) {
        ResponseMsg rm = new ResponseMsg();
        boolean b = recruitService.updApplicationStatus(applicationEntity);
        RecruitEntity recruitEntity = new RecruitEntity();
        recruitEntity.setR_id(applicationEntity.getR_id());
        if(applicationEntity.getA_status()==2){
            recruitEntity.setR_number(-1);
            recruitService.updRecruitNumber(recruitEntity);
        }else{
            recruitEntity.setR_number(1);
            recruitService.updRecruitNumber(recruitEntity);
        }
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("更改失败");
        }
        return rm;
    }

    //删除招聘
    @RequestMapping("delRecruit")
    @ResponseBody
    public ResponseMsg delRecruit(String ids) {
        ResponseMsg rm = new ResponseMsg();
        String[] r_ids;
        if (ids.contains(",")) {
            r_ids = ids.split(",");
        } else {
            r_ids = new String[]{ids};
        }
        boolean b = recruitService.delRecruit(r_ids);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
        }
        return rm;
    }

    //招聘状态更改
    @RequestMapping("updRecruitStatus")
    @ResponseBody
    public ResponseMsg updRecruitStatus(RecruitEntity recruitEntity) {
        ResponseMsg rm = new ResponseMsg();
        boolean b = recruitService.updRecruitStatus(recruitEntity);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("更改失败");
        }
        return rm;
    }

    //跳转到修改招聘界面
    @RequestMapping("recruitEdit")
    public String recruitEdit(RecruitEntity recruitEntity,HttpServletRequest request){
        RecruitEntity recruitEntity1 = recruitService.queryRecruit(recruitEntity);
        if(recruitEntity1 != null){
            request.setAttribute("recruit",recruitEntity1);
            return "recruitEdit";
        }else{
            return "error";
        }
    }

    //修改招聘
    @RequestMapping("updRecruit")
    @ResponseBody
    public ResponseMsg updrecruit(RecruitEntity recruitEntity, HttpServletRequest request){
        ResponseMsg rm = new ResponseMsg();
        boolean b = recruitService.updRecruit(recruitEntity);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

    //跳转到添加招聘界面
    @RequestMapping("recruitAdd")
    public String recruitAdd(){
        return "recruitAdd";
    }

    //添加招聘
    @RequestMapping("addRecruit")
    @ResponseBody
    public ResponseMsg addRecruit(RecruitEntity recruitEntity){
        ResponseMsg rm = new ResponseMsg();
        boolean regist = recruitService.addRecruit(recruitEntity);
        if (regist) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("添加失败");
        }
        return rm;
    }

}
